package com.piggy.logi.sys.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piggy.common.core.exception.GlobalException;
import com.piggy.common.core.utils.DateUtils;
import com.piggy.logi.sys.bo.LogiSysOrderSetEditBo;
import com.piggy.logi.sys.domain.LogiSysOrderSet;
import com.piggy.logi.sys.mapper.LogiSysOrderSetMapper;
import com.piggy.logi.sys.service.ILogiSysOrderSetService;
import com.piggy.logi.sys.vo.LogiSysOrderSetVo;
import groovy.util.logging.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Optional;

/**
 * 运单设置Service业务层处理
 */
@Slf4j
@Service
public class LogiSysOrderSetServiceImpl extends ServiceImpl<LogiSysOrderSetMapper, LogiSysOrderSet> implements ILogiSysOrderSetService {

    @Override
    public LogiSysOrderSetVo queryById(Long id) {
        LogiSysOrderSet orderSet = getOne(Wrappers.<LogiSysOrderSet>lambdaQuery().eq(LogiSysOrderSet::getDeptId, id));
        if (ObjectUtil.isEmpty(orderSet)) {
            return null;
        }

        LogiSysOrderSetVo orderSetVo = new LogiSysOrderSetVo();
        orderSetVo.setId(orderSet.getId());
        orderSetVo.setDeptId(orderSet.getDeptId());
        orderSetVo.setNoRuleSet(JSONUtil.toList(orderSet.getNoRuleSet(), LogiSysOrderSetVo.NoRuleSet.class));
        orderSetVo.setDetailSet(JSONUtil.toBean(orderSet.getDetailSet(), LogiSysOrderSetVo.DetailSet.class));
        orderSetVo.setOpenOrderSet(JSONUtil.toBean(orderSet.getOpenOrderSet(), LogiSysOrderSetVo.OpenOrderSet.class));
        orderSetVo.setInsuranceSet(JSONUtil.toBean(orderSet.getInsuranceSet(), LogiSysOrderSetVo.InsuranceSet.class));
        orderSetVo.setServiceFeeSet(JSONUtil.toBean(orderSet.getServiceFeeSet(), LogiSysOrderSetVo.ServiceFeeSet.class));
        return orderSetVo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertOrUpdate(LogiSysOrderSetEditBo bo) {
        LogiSysOrderSet update = BeanUtil.toBean(bo, LogiSysOrderSet.class);
        Optional.ofNullable(bo.getNoRuleSet()).ifPresent(item -> update.setNoRuleSet(JSONUtil.toJsonStr(item)));
        Optional.ofNullable(bo.getDetailSet()).ifPresent(item -> update.setDetailSet(JSONUtil.toJsonStr(item)));
        Optional.ofNullable(bo.getOpenOrderSet()).ifPresent(item -> update.setOpenOrderSet(JSONUtil.toJsonStr(item)));
        Optional.ofNullable(bo.getInsuranceSet()).ifPresent(item -> update.setInsuranceSet(JSONUtil.toJsonStr(item)));
        Optional.ofNullable(bo.getServiceFeeSet()).ifPresent(item -> update.setServiceFeeSet(JSONUtil.toJsonStr(item)));
        LogiSysOrderSetVo vo = this.queryById(bo.getDeptId());
        Optional.ofNullable(vo).ifPresent(item-> update.setId(vo.getId()));
        return saveOrUpdate(update);
    }

    @Override
    public String getOutCode(Long deptId) {
        LogiSysOrderSet orderSet = getOne(Wrappers.<LogiSysOrderSet>lambdaQuery().eq(LogiSysOrderSet::getDeptId, deptId));
        if (ObjectUtil.isEmpty(orderSet)) {
            throw new GlobalException("未配置运单规则");
        }
        List<LogiSysOrderSetEditBo.NoRuleSet> noRuleSetList = JSONUtil.toList(orderSet.getNoRuleSet(), LogiSysOrderSetEditBo.NoRuleSet.class);
        LogiSysOrderSetEditBo.NoRuleSet noRuleSet = noRuleSetList.stream().filter(item -> "1".equals(item.getIsCurrent()))
                .findAny().orElseThrow(() -> new GlobalException("未配置单号规则"));
        if (noRuleSet.getNoRuleType() == 1) {
            return noRuleSet.getNoRuleFirstLetter() + DateUtils.dateTimeNow() + RandomUtil.randomNumbers(5);
        } else if (noRuleSet.getNoRuleType() == 2) {
            return noRuleSet.getNoRuleFirstLetter() + IdUtil.getSnowflakeNextIdStr();
        } else {
            throw new GlobalException("单号规则配置异常");
        }
    }

}
